package logic;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Date;
import java.util.Vector;


public class myFile implements Serializable{
	/**
	 *  
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String id;
	private int numChunks;
	private Vector<Vector<String>> ips;
	private int replicationDeg;
	 
	public myFile()
	{
		 
		numChunks=0;
		setIps(new Vector<Vector<String>>());
		setReplicationDeg(0);
	}
	public myFile(String id)
	{
		 
		setIps(new Vector<Vector<String>>());
		numChunks=0;
		this.id=id;
		setReplicationDeg(0);
		
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getNumChunks() {
		return numChunks;
	}
	public void setNumChunks(int numChunks) {
		this.numChunks = numChunks;
	}
	 
	
	public void encoding() {
		 Date data =new Date();
		// convert String into InputStream
		InputStream is = new ByteArrayInputStream((name+data.toString()).getBytes());
	    String output = null;
	    int read;
	    byte[] buffer = new byte[8192];

	    try {
	        MessageDigest digest = MessageDigest.getInstance("SHA-256");
	        while ((read = is.read(buffer)) > 0) {
	            digest.update(buffer, 0, read);
	        }
	        byte[] hash = digest.digest();
	        BigInteger bigInt = new BigInteger(1, hash);
	        output = bigInt.toString(16);
	    } 
	    catch (Exception e) {
	        e.printStackTrace(System.err);
	        
	    }
	    id=output;
	}
	@Override 
	public boolean equals(Object o) {
		if(this.getId().equals(((myFile)o).getId()))
			return true;
		else return false;
	}
	public Vector<Vector<String>> getIps() {
		return ips;
	}
	public void setIps(Vector<Vector<String>> ips) {
		this.ips = ips;
	}
	public void ipsAdd(Vector<String> ip)
	{
		this.ips.add(ip);
	}
	public boolean removeIP(String ip,int chunkNo)
	{
		return ips.elementAt(chunkNo).remove(ip);
	}
	public int getReplicationDeg() {
		return replicationDeg;
	}
	public void setReplicationDeg(int replicationDeg) {
		this.replicationDeg = replicationDeg;
	}
	public String extractNameFile()
	{
		return name.substring(this.name.lastIndexOf("\\")+1);
	}

	
}
